package com.collect.tanzania.zhiUtils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ZHICalendarUtil {

    public static long mDateTimeOf0801L = ZHICalendarUtil.getTimeFromDateString("2021-08-01 00:00:00", null);
    public static long mDateTimeOf0706L = ZHICalendarUtil.getTimeFromDateString("2021-08-01 00:00:00", null);

    /**
     * 获取年
     */
    public static int getYear() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.YEAR);
    }

    /**
     * 获取月
     */
    public static int getMonth() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取月英文简写
     */
    public static String getMonthSimpleEng() {
        int month = getMonth() - 1;
        String[] months = new String[]{
                "Jan", "Feb", "Mar", "Apr",
                "May", "Jun", "July", "Aug",
                "Sep", "Oct", "Nov", "Dec"};
        if (month < 0 || months.length <= month) {
            month = 0;
        }
        return months[month];
    }

    /**
     * 根据英文简写获取月
     */
    public static int getMonthFromSimpleEng(String eng) {
        String[] months = new String[]{
                "Jan", "Feb", "Mar", "Apr",
                "May", "Jun", "July", "Aug",
                "Sep", "Oct", "Nov", "Dec"};
        for (int i = 0; i < months.length; i++) {
            String mon = months[i];
            if (mon.equalsIgnoreCase(eng)) {
                return (i + 1);
            }
        }

        return getMonth();
    }

    /**
     * 获取日
     */
    public static int getDay() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.DATE);
    }

    /**
     * 获取时
     */
    public static int getHour() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.HOUR);
    }

    /**
     * 获取分
     */
    public static int getMinute() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MINUTE);
    }

    /**
     * 获取当前时间的时间戳
     */
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前时间
     */
    public static String getCurrentTime() {
        return getDateTimeFormated(System.currentTimeMillis());
    }

    /**
     * 将long转换为日期 指定格式
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateFormated(long dateTime, String format) {
        String time = "";
        try {
            if(TextUtils.isEmpty(format)){
                format = "yyyy-MM-dd";
            }
            SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
            time = sDateFormat.format(new Date(dateTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将long转换为日期（yyyy-MM-dd）
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateFormated(long dateTime) {
        String time = "";
        try {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            time = sDateFormat.format(new Date(dateTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将long转换为日期（yyyy-MM-dd HH:mm）
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeFormated(long dateTime) {
        String time = "";
        try {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            time = sDateFormat.format(new Date(dateTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 将yyyy-MM-dd转换为日期
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getDateFromFormatString(String dateFormat) {
        Date date = null;
        try {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = sDateFormat.parse(dateFormat);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取当月第一天
     */
    public static String getCurMonthFirstDayFormatString() {
        String formatString = "";
        try {
            int month = ZHICalendarUtil.getMonth();
            int cday = ZHICalendarUtil.getDay();

            String monthString = month >= 10 ? String.valueOf(month) : ("0" + month);
            formatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + "01";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return formatString;
    }

    /**
     * 获取当月当前天
     */
    public static String getCurMonthCurDayFormatString() {
        String formatString = "";
        try {
            int month = ZHICalendarUtil.getMonth();
            int cday = ZHICalendarUtil.getDay();

            String monthString = month >= 10 ? String.valueOf(month) : ("0" + month);
            String cdayString = cday >= 10 ? String.valueOf(cday) : ("0" + cday);

            formatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + cdayString;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatString;
    }

    /**
     * 获取当前日期往后或往前n天
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateStringAfterDate(String dateString, int nextDays) {
        String tempStr = dateString;

        if (!TextUtils.isEmpty(dateString)) {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = simpleDateFormat.parse(dateString);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, nextDays);

                Date resultDate = calendar.getTime(); // 结果
                tempStr = simpleDateFormat.format(resultDate);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return tempStr;
    }

    /**
     * 获取日期字符串，以指定格式返回
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateStringWithFormat(String dateString, String oriFormatString, String targetFormatString) {
        String tempStr = dateString;

        if (!TextUtils.isEmpty(dateString)) {
            try {
                if (TextUtils.isEmpty(oriFormatString)) {
                    oriFormatString = "yyyy-MM-dd";
                }
                if (TextUtils.isEmpty(targetFormatString)) {
                    targetFormatString = "yyyy-MM-dd";
                }

                if (dateString.length() >= oriFormatString.length()) {
                    SimpleDateFormat oriFormat = new SimpleDateFormat(oriFormatString);
                    Date date = oriFormat.parse(dateString);

                    if (date != null) {
                        SimpleDateFormat targetFormat = new SimpleDateFormat(targetFormatString);
                        tempStr = targetFormat.format(date);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return tempStr;
    }

    /**
     * 获取指定日期的时间戳
     */
    @SuppressLint("SimpleDateFormat")
    public static long getTimeFromDateString(String dateString, String format) {

        long tempL = 0L;

        if (!TextUtils.isEmpty(dateString)) {
            try {
                if (TextUtils.isEmpty(format)) {
                    format = "yyyy-MM-dd HH:mm:ss";
                }

                SimpleDateFormat oriFormat = new SimpleDateFormat(format);
                Date date = oriFormat.parse(dateString);

                if (date != null) {
                    tempL = date.getTime();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return tempL;
    }

    public static void initDateTimeOf0801L() {
        mDateTimeOf0801L = ZHICalendarUtil.getTimeFromDateString("2021-08-01 00:00:00", null);
    }

    public static long getDateTimeOf0801L() {
        if (mDateTimeOf0801L < 10L) {
            initDateTimeOf0801L();
        }

        return mDateTimeOf0801L;
    }

    public static void initDateTimeOf0706L() {
        mDateTimeOf0706L = ZHICalendarUtil.getTimeFromDateString("2021-07-06 00:00:00", null);
    }

    public static long getDateTimeOf0706L() {
        if (mDateTimeOf0706L < 10L) {
            initDateTimeOf0706L();
        }

        return mDateTimeOf0706L;
    }

}
